<?php
/**
 * Default page
 *
 * @author Lukas Normantas
 */
class Content_Model_Page extends Doctrine_Record{
    public function setTableDefinition()
    {
        $this->setTableName('content_pages');
        $this->hasColumn('title', 'string', 50);
        $this->hasColumn('body', 'clob');
        $this->hasColumn('active', 'boolean', 25);
        $this->hasColumn('type', 'string', 50);
        $this->hasColumn('position', 'integer', 11, array('notnull'=>false));

        $this->setSubclasses(array(
                'Content_Model_Article'  => array('type' => 'article'),
                'Content_Model_Help' => array('type' => 'help')
            )
        );
    }

    public function setUp()
    {
        $this->actAs('Timestampable');
        $sortable = new Areal_Doctrine_Template_Sortable();
        $this->actAs($sortable);
    }
    
  public function setPosition($p)
  {
      $this->position = $p;
      return true;
  }

    public function getAll()
    {
        $q = Doctrine_Query::create()
               ->from( get_class($this) . ' u' )
               ->orderBy('position');
        $result = $q->execute(array(), Doctrine::HYDRATE_RECORD);
        return $result;
    }

    public function getActivePageList()
    {
        $q = Doctrine_Query::create()
               ->from( get_class($this) . ' p' )
               ->addWhere('p.active = ? ', 1)
               ->orderBy('position');
        $result = $q->execute(array(), Doctrine::HYDRATE_RECORD);
        return $result;
    }


    protected $_editingForm = null;

    public function getEditingForm(){
        if ($this->_editingForm == null){
            $this->_editingForm = new Content_Form_PageEditing();
            $this->_editingForm->setDefaults($this->toArray());
        }
        return $this->_editingForm;
    }

    public function edit($data)
    {
        $form = $this->getEditingForm();
        if ($form->isValid( $data )){
            $this->fromArray($form->getValues());
            $this->save();       
            return true;
        }
        return false;
    }




}

